<script setup lang="ts">
import useImgToUrl from '@/components/imgToUrl'
const {
  input,
  dropElement,
  imgsList,
  dragEnter,
  dragLeave,
  drop,
  clickInput,
  selectFiles,
  copyUrl,
  removeImg
} = useImgToUrl()
</script>

<template>
  <div>
    <input
      ref="input"
      @change="selectFiles($event as InputEvent)"
      type="file"
      name=""
      id=""
      v-show="false"
      multiple
      accept="image/*"
    />
    <p
      class="drop"
      ref="dropElement"
      @click="clickInput"
      @dragover.prevent
      @dragenter="dragEnter"
      @dragleave="dragLeave"
      @drop.prevent="drop($event)"
    >
      <Picture class="files" />
    </p>
    <ul id="imgs">
      <li v-for="(url, index) in imgsList" :key="index">
        <img :src="url" alt="" />
        <span tabindex="0" class="img-copy" @click="copyUrl(index)">
          <CopyDocument class="copy" />
        </span>
        <span tabindex="0" class="img-close" @click="removeImg(index)">
          <Close class="close" />
        </span>
      </li>
    </ul>
  </div>
</template>

<style scoped lang="less">
div {
  width: 100%;
  height: 600px;
  background-color: var(--color1);
  border-radius: var(--radius);
  overflow: hidden;
}
#imgs {
  max-height: 400px;
  overflow-y: scroll;
  padding: 10px;
  li:hover {
    border-color: var(--color3);
  }
  li {
    margin-bottom: 10px;
    border: 2px dashed var(--color2);
    border-radius: var(--radius);
    overflow: hidden;
    display: grid;
    grid-template-columns: 42px 1fr 40px;
    align-items: center;
    > img {
      width: 42px;
      height: 42px;
    }

    .img-copy {
      width: 100%;
      height: 100%;
      position: relative;
      text-align: center;
      .copy {
        position: absolute;
        top: 50%;
        left: 50%;
        translate: -50% -50%;
        width: 20px;
        height: 20px;
      }
    }
    .img-close {
      padding: 10px;
      justify-self: end;
      .close {
        width: 20px;
        height: 20px;
      }
    }

    > span:hover {
      background-color: var(--color2);
      color: var(--color3);
    }
    > span {
      cursor: pointer;
      color: var(--color2);
    }
  }
}
.drop {
  cursor: pointer;
  position: relative;
  height: 100px;
  width: 200px;
  border: 2px dashed var(--color3);
  margin: 10px;
  box-sizing: border-box;
  color: var(--color2);
  border-radius: var(--radius);
}
.files {
  position: absolute;
  top: 50%;
  left: 50%;
  translate: -50% -50%;
  width: 60px;
  height: 60px;
  pointer-events: none;
}

.dragover {
  border: 2px dashed var(--color1);
  background-color: var(--color3);
}
</style>
